<template>
  <div style="height: 100vh; display: flex; align-items: center; justify-content: center;">
    <div style="display: flex; background-color: white; width: 50%; border-radius: 6px; overflow: hidden">
      <div style="flex: 1;">
        <img src="@/assets/logo.png" alt="" style="width: 100%">
      </div>
      <div style="flex: 1; display: flex; align-items: center; justify-content: center;">
        <el-form :model="user" :rules="rules" ref="registerRef" @submit.native.prevent style="width: 75%">
          <div style="font-size: 20px; font-weight: bold; text-align: center; margin-bottom: 20px">欢迎注册管理系统
          </div>
          <el-form-item prop="username">
            <el-input prefix-icon="el-icon-user" placeholder="请输入用户名" v-model="user.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input prefix-icon="el-icon-lock" show-password placeholder="请输入密码"
                      v-model="user.password"></el-input>
          </el-form-item>
          <el-form-item prop="confirmPass">
            <el-input prefix-icon="el-icon-lock" show-password placeholder="请确认密码"
                      v-model="user.confirmPass"></el-input>
          </el-form-item>
          <el-form-item prop="role">
            <el-radio-group v-model="user.role">
              <el-radio label="用户"></el-radio>
              <el-radio label="商家"></el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" native-type="submit" @click="register" style="width: 100%">注 册</el-button>
          </el-form-item>
          <div style="display: flex; justify-content: space-between;">
            <div>已经有账号？请 <span style="color: blue; cursor: pointer;" @click="$router.push('/login')">登录</span>
            </div>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>

export default {
  name: 'RegisterView',
  data() {
    var validatePassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请确认密码'))
      } else if (value.toLowerCase() !== this.user.password.toLowerCase()) {
        callback(new Error('密码不一致'))
      } else {
        callback()
      }
    }
    return {
      user: {
        username: '',
        password: '',
        confirmPass: '',
        role: '',
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          // {min:6,max:18,message:'密码长度为6到18位',trigger:'blur'}
        ],
        confirmPass: [
          {validator: validatePassword, trigger: 'blur'}
        ],
        role: [
          {required: true, message: '请选择角色', trigger: 'blur'}
        ],
      }
    }
  },
  created() {
  },
  methods: {
    register() {
      this.$refs['registerRef'].validate((valid) => {
        if (valid) {
          this.$request.post('/register', this.user).then(res => {
            if (res.code === '200') {
              this.$message.success('注册成功')
              this.$router.push('/login')
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
